<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once(APPPATH . '/controllers/super_admin' . EXT);
class Score extends Super_admin {

    function __construct() {
        parent::__construct();

        $this->load->model(array(
            'Group_model',
            'User_model',
            'Exam_group_model',
            'Exam_model',
            'Exam_detail_model',
            'Score_model',
            'Score_detail_model',
        ));

        $this->load->helper(array(
            'security',
            'string',
        ));
    }
    /**
     * list_records
     *
     * Load information list
     *
     * @access public
     * @param int
     * @return void
     */
    function list_records($group_id) {
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        $data['group'] =  $this->Group_model->get_record(array('where' => 'group_id = "'.$group_id.'"'));

        $this->load->view('score/list',$data);
    }
    /**
     * view_myscore
     *
     * Load information list
     *
     * @access public
     * @param int
     * @param int
     * @return void
     */
    function view_myscore($group_id) {
        $access = array('student');
        $this->_is_authorize($access); //check super_admin controller

        $data['group'] =  $this->Group_model->get_record(array('where' => 'group_id = "'.$group_id.'"'));

        $this->load->view('score/my_exam_score_list',$data);
    }
    /**
     * view_score
     *
     * Load information list
     *
     * @access public
     * @param int
     * @param int
     * @return void
     */
    function view_score($group_id, $exam_id) {
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        $data['group'] =  $this->Group_model->get_record(array('where' => 'group_id = "'.$group_id.'"'));
        $data['exam'] =  $this->Exam_model->get_record(array('where' => 'exam_id = "'.$exam_id.'"'));

        $this->load->view('score/exam_score_list',$data);
    }
    /**
     * view_score_detail
     *
     * Load information list
     *
     * @access public
     * @param int
     * @param int
     * @return void
     */
    function view_score_detail($score_id) {
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        $data['score'] =  $this->Score_model->get_record(array('where' => 'score_id = "'.$score_id.'"'));
        $data['exam'] =  $this->Exam_model->get_record(array('where' => 'exam_id = "'.$data['score']['exam_id'].'"'));
        $data['exam_group'] =  $this->Exam_group_model->get_record(array('where' => 'exams_exam_id = "'.$data['score']['exam_id'].'"'));
        $data['user'] =  $this->User_model->get_record(array('where' => 'user_id = "'.$data['score']['user_id'].'"'));
        $data['score_details'] =  $this->Score_detail_model->get_list(array('where' => 'score_id = "'.$score_id.'"'));
        $this->load->view('score/exam_score_details', $data);
    }
    /**
     * view_myscore_detail
     *
     * Load information list
     *
     * @access public
     * @param int
     * @param int
     * @return void
     */
    function view_myscore_detail($score_id) {
        $access = array('student');
        $this->_is_authorize($access); //check super_admin controller

        $data['score'] =  $this->Score_model->get_record(array('where' => 'score_id = "'.$score_id.'"'));
        $data['exam'] =  $this->Exam_model->get_record(array('where' => 'exam_id = "'.$data['score']['exam_id'].'"'));
        $data['exam_group'] =  $this->Exam_group_model->get_record(array('where' => 'exams_exam_id = "'.$data['score']['exam_id'].'"'));
        $data['user'] =  $this->User_model->get_record(array('where' => 'user_id = "'.  _session('user_id').'"'));
        $data['score_details'] =  $this->Score_detail_model->get_list(array('where' => 'score_id = "'.$score_id.'"'));
        $this->load->view('score/exam_myscore_details', $data);
    }
    function print_score_detail () {
        $exam_id = $this->input->post('exam_id', TRUE);

        $this->load->view('print/score_detail');

    }
    /**
     * get_exams
     *
     * get exam list
     *
     * @access public
     * @return string
     */
    function get_exams() {

        $search = $this->input->post('search', TRUE);
        $date_posted = $this->input->post('date_posted', TRUE);
        $date_posted = ita_to_mysql($date_posted);
        
        $group_id = $this->input->post('group_id', TRUE);

        $where = 'e.exam_id != "!@#" AND e.status = "Closed"';

        $join_exam_group = array(
            'table' => EXAMS_GROUPS . ' eg',
            'condition' => 'eg.exams_exam_id = e.exam_id AND eg.groups_group_id = "'.$group_id.'"',
        );

        //Filter by
        $filter = ( ! empty($search) )  ? 'e.exam_name LIKE "%' .$search.'%" ' : NULL;  //lesson name
        $filter .= ( $date_posted != '0000-00-00' && ! empty($search) ) ? ' AND e.date_posted = "' .$date_posted.'" ' : NULL;  //with exam name
        $filter .= ( $date_posted != '0000-00-00' &&  empty($search) ) ? ' e.date_posted = "' .$date_posted.'" ' : NULL;  //without exam name
    
        $where .= ( ! empty($filter) ) ? ' AND  '.$filter : NULL;

        $select = 'e.exam_id, e.exam_name, eg.groups_group_id, e.exam_kind, e.date_posted, e.status';

        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'join' => array($join_exam_group),
                    'order_by' => $order_by,
                    'group_by' => 'e.exam_id'
        );

        //For pagination
        $page = $this->input->post('page');
        $total = count($this->Exam_model->get_list($args));

        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        //PUSH THIS VALUE INTO ARGS
        $args['limit'] = LIMIT;
        $args['offset'] = $offset;
        $args['resource_id'] = TRUE;
        //end Pagination

        $records = $this->Exam_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'exam_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }
    /**
     * get_my_score
     *
     * get exam score list
     *
     * @access public
     * @return string
     */
    function get_my_score() {

        $search = $this->input->post('search', TRUE);
        $date_posted = $this->input->post('date_posted', TRUE);
        $date_posted = ita_to_mysql($date_posted);

        $group_id = $this->input->post('group_id', TRUE);

        $where = 'e.exam_id != "!@#" AND ug.user_id ="'.  _session('user_id'). '"';

        $join_score = array(
            'table' => SCORES . ' s',
            'condition' => 's.exam_id = e.exam_id AND s.user_id = "'._session('user_id') .'"',
            'join' => 'LEFT'
        );
        $join_exam_group = array(
            'table' => EXAMS_GROUPS . ' eg',
            'condition' => 'eg.exams_exam_id = e.exam_id AND eg.groups_group_id ="'.$group_id.'"',
        );
        $join_user_group = array(
            'table' => USERS_GROUPS . ' ug',
            'condition' => 'ug.group_id = eg.groups_group_id',
        );


        //Filter by
        $filter = ( ! empty($search) )  ? 'e.exam_name LIKE "%' .$search.'%" ' : NULL;  //lesson name
        $filter .= ( $date_posted != '0000-00-00' && ! empty($search) ) ? ' AND e.date_posted = "' .$date_posted.'" ' : NULL;  //with exam name
        $filter .= ( $date_posted != '0000-00-00' &&  empty($search) ) ? ' e.date_posted = "' .$date_posted.'" ' : NULL;  //without exam name

        $where .= ( ! empty($filter) ) ? ' AND  '.$filter : NULL;

        $select = 'e.exam_id,
            s.score_id,
            e.exam_name,
            IF(ISNULL(s.actual_score), "0", s.actual_score) actual_score,
            e.no_of_terms,
            IF(ISNULL(s.score_id), "Not Taken", "Taken") is_exam_taken,
            e.date_posted';

        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'join' => array($join_score, $join_exam_group, $join_user_group),
                    'order_by' => $order_by,
        );

        //For pagination
        $page = $this->input->post('page');
        $total = count($this->Exam_model->get_list($args));

        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        //PUSH THIS VALUE INTO ARGS
        $args['limit'] = LIMIT;
        $args['offset'] = $offset;
        $args['resource_id'] = TRUE;
        //end Pagination

        $records = $this->Exam_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'exam_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }

    /**
     * get_exam_student_scores
     *
     * get exam student score list
     *
     * @access public
     * @return string
     */
    function get_exam_student_score() {

        $search = $this->input->post('search', TRUE);
        $exam_id = $this->input->post('exam_id', TRUE);

        $where = 'u.user_id != "!@#" AND u.user_type = "student"';

        $join_exam= array(
            'table' => EXAMS . ' e',
            'condition' => 'e.exam_id = "'.$exam_id.'"',
        );
        $join_exam_group = array(
            'table' => EXAMS_GROUPS . ' eg',
            'condition' => 'eg.exams_exam_id = e.exam_id',
        );
        $join_user_group = array(
            'table' => USERS_GROUPS . ' ug',
            'condition' => 'ug.group_id = eg.groups_group_id AND ug.user_id = u.user_id',
        );
        $join_score = array(
            'table' => SCORES . ' s',
            'condition' => 's.exam_id = e.exam_id AND s.user_id = u.user_id',
            'join' => 'LEFT',
        );

        //Filter by
        $filter = ( ! empty($search) )  ? 'CONCAT(u.lname, ", ", u.fname, " ", u.mname) LIKE "%' .$search.'%" ' : NULL;  //student name
        $where .= ( ! empty($filter) ) ? ' AND  '.$filter : NULL;

        $select = 'u.user_id,
            s.score_id,
            e.exam_id,
            CONCAT(u.lname, ", ", u.fname, " ", u.mname) student_name,
            IF(ISNULL(s.actual_score), "0", s.actual_score) actual_score,
            e.no_of_terms,
            IF(ISNULL(score_id), "Not Taken", "Taken") is_exam_taken';

        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'join' => array($join_exam, $join_exam_group, $join_user_group, $join_score),
                    'order_by' => $order_by,
        );

        //For pagination
        $page = $this->input->post('page');
        $total = count($this->User_model->get_list($args));

        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        //PUSH THIS VALUE INTO ARGS
        $args['limit'] = LIMIT;
        $args['offset'] = $offset;
        $args['resource_id'] = TRUE;
        //end Pagination

        $records = $this->User_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'user_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }
   
}
/* End of file score.php */
/* Location: ./application/controllers/score.php */
